<?

  Prado::using('Application.pages.utils.CommonUtils');
 class Search extends TPage
 {
	public $search_str = "";
	public $id = -1;
	public $friend = null;
	public function onInit($param)
    {	


		$this->search_str = $this->Request['q'];
		$this->id = $this->user->ID;
		$this->initFriendList();
		$this->initResultSearch();

	}
	public function initFriendList()
	{
		if($this->user->ID != null) // user logged in
		{
			$this->friend = $this->getFriendIDs();
		}			
		else // user doesn't login
		{
			echo "<b>NO</b>";
		}
	}
	
	public function getFriendIDs()
	{
		$ret = array();

		/////  Find all possible friends ids		
		/// search user_id , use friend_id
		$rec = FriendRecord::finder()->findAll('user_id = ?',$this->id);
			
		for($i = 0;$i < sizeof($rec);$i++)
		{
			array_push($ret,$rec[$i]->friend_id);
	

		}
		/// search friend_id , use user_id
		$rec2 = FriendRecord::finder()->findAll('friend_id = ?',$this->id);
		for($i = 0;$i < sizeof($rec2);$i++)
		{	
			array_push($ret,$rec2[$i]->user_id);
		}	
		return $ret;	
	}
	
	public function initResultSearch()
	{
		$arr_result = $this->getResultID();
		$this->result->Text = sizeof($arr_result)." Results found for: ". $this->search_str;
//		print_r($arr_result);
//		for($i = 0;$i < sizeof($arr_result);$i++)
//		{
//			$this->result->Text .= "<br/>ID: ". $arr_result[$i];
//		}
		//###### Processing Renderer...
		
		$this->repeater_users->VirtualItemCount = sizeof($arr_result);
        $this->populateSearchData();
		
	}
	
	protected function populateSearchData()
    {
        $offset=$this->repeater_users->CurrentPageIndex*$this->repeater_users->PageSize;
        $limit=$this->repeater_users->PageSize;
        if($offset+$limit > $this->repeater_users->VirtualItemCount)
            $limit=$this->repeater_users->VirtualItemCount-$offset;
        $this->repeater_users->DataSource=$this->getRequests($offset,$limit);
        $this->repeater_users->dataBind();
	//	echo $this->Repeater->VirtualItemCount;
    }
	
	protected function getRequests($offset, $limit)
    {
        // Construts a query criteria
        $criteria=new TActiveRecordCriteria;
		//$owner = UserRecord::finder()->findByEmail($this->user->Email);
		
		$criteria->Condition = "username like \"%". $this->search_str . "%\" ";
//		$criteria->Parameters[':user_id'] = $this->id;
        $criteria->OrdersBy['username']='desc';
        $criteria->Limit=$limit;
        $criteria->Offset=$offset;
	//	echo $criteria;
		//return;
        // query for the posts with the above criteria and with author information
        return UserRecord::finder()->findAll($criteria);
    }
	
	public function pageChanged_search($sender,$param)
    {
        // change the current page index to the new one
        $this->repeater_users->CurrentPageIndex=$param->NewPageIndex;
        // re-populate data into the repeater
        $this->populateSearchData();
    }
	
	public function getCurrentRecord()
	{
	
	//	$userRecord = UserRecord::finder()->findByEmail($this->user->Email); $this->id
		$userRecord = UserRecord::finder()->findByUser_Id($this->id);
		return $userRecord;
	}
	
	public function getResultID()
	{
	/*
		$dsn = "mysql:host=localhost;dbname=sbdb";
		$username=  "root";
		$password = "winwinwin";
		
		$connection = new TDbConnection($dsn,$username,$password);
		// call setAttribute() to pass in additional connection parameters
		// $connection->Persistent=true;  // use persistent connection
		$connection->Active=true;  // connection is established
		
		$sql = "select user_id from tblusers where username like \"%". $this->search_str . "%\" ";

		$command=$connection->createCommand($sql);
		$dataReader=$command->query();   
		$arr = array();
		while(($row=$dataReader->read())!==false) { 
			
			array_push($arr ,$row["user_id"]);
		}
		$connection->Active = false;  // connection is closed*/
		$arr = array();
		
		$rec = UserRecord::finder()->findAll("username like \"%". $this->search_str . "%\" ","");
		
		
		
		for($i = 0;$i < sizeof($rec);$i++)
		{
			array_push($arr,$rec[$i]->user_id);
		

		}
	
		return $arr;
	}
	
	public function getAvatar($user)
	{
		$img_path = $this->Application->Parameters['ImageFolderName'];
		return CommonUtils::getAvatar($img_path,$user);		
	}
 }
 ?>